<?php
    $q = $_GET["q"];
    if($q == 0){//index的数据
        $a = mysqli_connect("localhost","root","gala123456");
        $db = mysqli_select_db($a,"sheep");
        $sql = "SELECT `business_address` FROM `business`";
        $query = mysqli_query($a,$sql);
        $address = array();
        while ($row = mysqli_fetch_row($query)) {
            $ding = 0;
            foreach ($row as $nei) {
                for ($i = 0;$i < count($address);$i++) {
                    if ($address[$i] == $nei) {
                        $ding++;
                    }
                }
                if ($ding == 0) {
                    array_push($address,$nei);
                }
            }
        }
        $num_arr = array();
        $url = array();
        for ($i = 0;$i < count($address);$i++) {
            $sql = "SELECT `business_sheepID` FROM `business` WHERE `business_address` = '".$address[$i]."'";
            $query = mysqli_query($a,$sql);
            $num = 0;
            while ($row = mysqli_fetch_row($query)) {
                foreach ($row as $nei) {
                    $new_nei = explode("-",$nei);
                    for ($j = 0;$j < count($new_nei);$j++) {
                        $sql = "SELECT `sheep_select` FROM `sheep` WHERE `sheepID` = '".$new_nei[$j]."'";
                        $query = mysqli_query($a,$sql);
                        while ($row2 = mysqli_fetch_row($query)) {
                            foreach ($row2 as $nei2) {
                                if ($nei2 != null) {
                                    $num++;
                                }
                            }
                        }
                    }
                }
            }
            array_push($num_arr,$num);
            $sql2 = "SELECT `address_img` FROM `address_img` WHERE `name` = '".$address[$i]."'";
            $query2 = mysqli_query($a,$sql2);
            while ($row2 = mysqli_fetch_row($query2)) {
                foreach ($row2 as $nei2) {
                    array_push($url,$nei2);
                }
            }
        }
        $arr = array(
            "address" => $address,
            "num_select" => $num_arr,
            "address_url" => $url
        );
        echo json_encode($arr);
    }
    mysqli_close($a);
?>